/**
 * 自定义Hook
 */

import { useState, useMemo, useEffect } from 'react'
import { Button } from 'antd'

function usePartial(initialValue) {
  let [state, setState] = useState(initialValue)

  function setPartial(partialValue) {
    setState({
      ...state,
      partialValue
    })
  }

  return [state, setPartial]
}

function useDidMount(title) {
  title = title || 'react'
  useEffect(() => {
    document.title = title
  }, [])
}

function Demo() {
  let [state, setPartial] = usePartial({
    sup: 5,
    opp: 10
  })

  useDidMount('测试 标题')

  let handle = function handle(type) {
    switch (type) {
      case 'sup':
        setPartial({
          sup: state.sup++
        })
        break;
      case "opp":
        setPartial({
          opp: state.opp++
        })
        break
    }
  }

  return <div>
    <div id='num'>支持数：{state.sup}</div>
    <div id='num'>反对数：{state.opp}</div>
    <div>
      <Button onClick={handle.bind(null, 'sup')}>支持数 +1</Button>
      <Button danger onClick={handle.bind(null, 'opp')}>反对数 +1</Button>
    </div>
  </div>
}

export default Demo 